package com.yj.suncity.user;

import java.util.List;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.SeeAdvDetailActivity;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.bean.AdvBean;
import com.yj.suncity.bean.AdvListBean;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.PullToRefreshView;
import com.yj.suncity.utis.ToastUtil;
import com.yj.suncity.utis.PullToRefreshView.OnFooterRefreshListener;
import com.yj.suncity.utis.PullToRefreshView.OnHeaderRefreshListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class UserSeeAdvListActivity extends Activity implements
		OnClickListener, OnFooterRefreshListener, OnHeaderRefreshListener {

	private Context context;
	private String userId;
	private String titleName;
	private boolean mRefreshHeadFlag = true;// 判断是否刷新的是头部(刚开始默认是头部刷新)
	private boolean mRefreshFlag = false;// 判断是否刷新
	private PullToRefreshView mPullToRefreshView = null;
	private int pageNum = 1;
	private ListView mList_see_adv;
	private SeeAdvAdapter adapter;
	private List<AdvBean> detailInfos;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_see_adv);
		context = this;
		userId = getIntent().getStringExtra("userid");
		titleName = getIntent().getStringExtra("titlename");
		findView();
		initData(1);
	}

	private void findView() {
		((TextView) findViewById(R.id.txt_title_name)).setText(titleName);
		findViewById(R.id.lay_rela_order).setVisibility(View.GONE);
		findViewById(R.id.btnHeadBack).setOnClickListener(this);
		findViewById(R.id.txt_btn_all).setOnClickListener(this);
		findViewById(R.id.txt_btn_time).setOnClickListener(this);
		findViewById(R.id.txt_btn_price).setOnClickListener(this);
		findViewById(R.id.txt_btn_moods).setOnClickListener(this);

		mPullToRefreshView = (PullToRefreshView) findViewById(R.id.main_pull_refresh_view);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		TextView emptyView = (TextView) findViewById(R.id.txt_empty);
		mList_see_adv = (ListView) findViewById(R.id.list_see_adv);
		mList_see_adv.setEmptyView(emptyView);
	}

	private void initData(int i) {

		String path = URLCon.广告 + "?action=seemyadvlist&pagesize=20&pageindex="
				+ i + "&userid=" + userId;
		Log.d("", path);
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null,
				mPullToRefreshView, mRefreshFlag, null, true, "正在加载......",
				new AsyncTaskUtil.ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						AdvListBean listBean = new AdvListBean(jsonObject);
						pageNum = Integer.valueOf(listBean.getTotalpage());
						if (mRefreshHeadFlag) {
							adapter = null;
						}
						if (adapter == null) {
							detailInfos = listBean.getData();
							adapter = new SeeAdvAdapter(detailInfos);
							mList_see_adv.setAdapter(adapter);
						} else {
							detailInfos.addAll(listBean.getData());
							adapter.notifyDataSetChanged();
						}
					}
				});
		asyncTaskUtil.execute(path);

	}

	class SeeAdvAdapter extends BaseAdapter {
		private List<AdvBean> advBeans;

		public SeeAdvAdapter(List<AdvBean> advBeans) {
			super();
			imageLoader = ImageLoader.getInstance();
			this.advBeans = advBeans;
		}

		@Override
		public int getCount() {
			return advBeans.size();
		}

		@Override
		public Object getItem(int position) {
			return advBeans.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.adapter_see_adv_list, null);
				holder.mImg_logo = (ImageView) convertView
						.findViewById(R.id.img_logo);
				holder.mTxt_name = (TextView) convertView
						.findViewById(R.id.txt_shop_name);
				holder.mTxt_jianjie = (TextView) convertView
						.findViewById(R.id.txt_shop_jianjie);
				holder.mTxt_all_totle = (TextView) convertView
						.findViewById(R.id.txt_all_num);
				holder.mTxt_give_num = (TextView) convertView
						.findViewById(R.id.txt_give_num);
				holder.mLay_linear_adv_item = (LinearLayout) convertView
						.findViewById(R.id.lay_linear_adv_item);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final AdvBean advBean = advBeans.get(position);
			holder.mTxt_name.setText(advBean.getEntname());
			holder.mTxt_jianjie.setText(advBean.getEnt_jianjie());
			holder.mTxt_all_totle.setText(advBean.getAmount());
			holder.mTxt_give_num.setText(advBean.getSurplus_num());
			if (!advBean.getEnt_logo().equals("")) {
				imageLoader.displayImage(URLCon.HOST + advBean.getEnt_logo(),
						holder.mImg_logo, options);
			} else {
				holder.mImg_logo
						.setImageResource(R.drawable.android_layout_detail);
			}
			holder.mLay_linear_adv_item
					.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {

							Intent intent = new Intent(context,
									SeeAdvDetailActivity.class);
							intent.putExtra("adv_userid", advBean.getUserid());
							intent.putExtra("advid", advBean.getAid());
							intent.putExtra("flag", 1);
							startActivity(intent);
						}
					});
			return convertView;
		}
	}

	class ViewHolder {
		ImageView mImg_logo;
		TextView mTxt_name;
		TextView mTxt_jianjie;
		TextView mTxt_all_totle;
		TextView mTxt_give_num;
		LinearLayout mLay_linear_adv_item;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;

		default:
			break;
		}

	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		mPullToRefreshView.postDelayed(new Runnable() {
			@Override
			public void run() {
				mRefreshHeadFlag = false;
				mRefreshFlag = true;

				++indexPage;
				if (indexPage > pageNum) {
					ToastUtil.ShowToast(context, "没有更多数据！");
					mPullToRefreshView.onFooterRefreshComplete();
					return;
				} else {
					initData(indexPage);
				}

				// mPullToRefreshView.onFooterRefreshComplete();
			}
		}, 1);

	}

	int indexPage = 1;

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		mPullToRefreshView.postDelayed(new Runnable() {
			@Override
			public void run() {
				mRefreshHeadFlag = true;
				mRefreshFlag = true;
				// if (async != null) {
				// Utils.cancelTaskInterrupt(async);
				// async = null;
				// }
				indexPage = 1;// 刷新头部时将页数初始化为1
				initData(indexPage);
				// mPullToRefreshView.onHeaderRefreshComplete();
			}
		}, 1);
	}

}
